package org.ohos.sample.cordova;

import com.ffsky.litepack.logger.L;
import ohos.agp.components.webengine.AsyncCallback;
import ohos.agp.components.webengine.Navigator;
import ohos.agp.components.webengine.WebConfig;
import ohos.agp.components.webengine.WebView;
import ohos.app.Context;
import org.ffsky.jsplugin.JsPlugin;
import org.ffsky.jsplugin.JsPluginCompat;
import org.ffsky.jsplugin.JsPluginManager;
import org.ohos.sample.cordova.util.WebSettingsCompat;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

public class SystemWebViewEngine extends WebViewEngine {

    final String TAG="SystemWebViewEngine";
    private WebView mWebView;

    public SystemWebViewEngine(Context context, WebViewInterface webViewInterface) {
        super(context, webViewInterface);
    }

    public void init(WebView webView){
        mWebView = webView;
        settingsWebView();
    }

    List<JsPlugin> jsPlugins = new ArrayList();
    private HarExposedJsApi mHarExposedJsApi;
    private SystemWebAgent mSystemWebAgent;
    private void settingsWebView(){
        WebSettingsCompat.settings(mWebView);
        mSystemWebAgent = new SystemWebAgent(webViewInterface);
        mWebView.setWebAgent(mSystemWebAgent);
        mWebView.setBrowserAgent(new SystemBrowserAgent(getContext(),webViewInterface));
        mHarExposedJsApi = new HarExposedJsApi(this);
        //
        Set<Class<? extends JsPlugin>> list = JsPluginManager.getInstance().listJsPlugin();
        if(list!=null){
            for(Class<? extends JsPlugin> cls:list){
                try {
                    JsPlugin createJsPlugin = cls.newInstance();
                    jsPlugins.add(createJsPlugin);
                    createJsPlugin.bindJsBridge(mWebView);
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                } catch (InstantiationException e) {
                    e.printStackTrace();
                }
            }
        }
        JsPluginCompat.create(jsPlugins,webViewInterface.getAbility(),mHarExposedJsApi);

    }
    @Override
    public void load(String url) {
        mWebView.load(url);
    }

    @Override
    public String getUrl() {
        return mSystemWebAgent.getPageLoadedUrl();
    }
    @Override
    public void evaluateJavascript(String js) {
            if(mWebView!=null){
                mWebView.executeJs(js, new AsyncCallback<String>() {
                    @Override
                    public void onReceive(String s) {
                        L.i(TAG,"evaluateJavascript->onReceive->",s);
                    }
                });
            }
    }

    @Override
    public boolean canGoBack() {
        Navigator navigator = mWebView.getNavigator();
       return navigator.canGoBack();
    }

    @Override
    public void goBack() {
        Navigator navigator = mWebView.getNavigator();
        if (navigator.canGoBack()) {
            navigator.goBack();
        }
    }

    @Override
    public void goForward() {
        Navigator navigator = mWebView.getNavigator();
        if (navigator.canGoForward()) {
            navigator.goForward();
        }
    }

    @Override
    public boolean canGoForward() {
        Navigator navigator = mWebView.getNavigator();
        if (navigator.canGoForward()) {
            return true;
        }
        return  false;
    }

    @Override
    public void destroy() {
        JsPluginCompat.destroy(jsPlugins);
        if(mWebView!=null){
            mWebView.load("about:blank");
        }
    }
}
